#!/bin/bash
set -e
GreenBG="\\033[42;37m"
YellowBG="\\033[43;37m"
BlueBG="\\033[44;37m"
Font="\\033[0m"
Version="${BlueBG}[版本]${Font}"
Info="${GreenBG}[信息]${Font}"
Warn="${YellowBG}[提示]${Font}"
cd /app
echo -e "\n ================ \n ${Info} ${GreenBG} 欢迎使用lx-sync-server-env ${Font} \n ================ \n"
if [ "`dir -A /app/server`" = "" ]; then
    echo -e "\n ================ \n ${Info} ${GreenBG} 即将安装lx-sync-server ${Font} \n ================ \n"
    git clone https://gitee.com/yi0086/lx-mirror /app/server
else
    if [[ ! -d "/app/server/.git" ]]; then
        echo -e "\n ================ \n ${Info} ${GreenBG} 不存在.git,无法更新 ${Font} \n ================ \n"
    else
        set +e
        git remote update &> /dev/null
        LOCAL=$(git rev-parse @)
        REMOTE=$(git rev-parse @{u})
        BASE=$(git merge-base @ @{u})
        if [ $LOCAL = $REMOTE ]; then
            echo "Up-to-date"
        elif [ $LOCAL = $BASE ]; then
            echo -e "\n ================ \n ${Info} ${GreenBG} 即将更新lx-sync-server ${Font} \n ================ \n"
            git -C /app/server pull
            cd /app/server
            echo -e "\n ================ \n ${Info} ${GreenBG} 即将安装依赖 ${Font} \n ================ \n"
            npm install
            echo -e "\n ================ \n ${Info} ${GreenBG} 即将编译server ${Font} \n ================ \n"
            npm run build
        else
            echo -e "\n ================ \n ${Info} ${GreenBG} 存在冲突,不更新 ${Font} \n ================ \n"
        fi
        set -e
    fi
fi
cd /app/server
echo -e "\n ================ \n ${Info} ${GreenBG} 即将启动lx-sync-server ${Font} \n ================ \n"
npm run prd
pm2 logs lx-music-sync-server